﻿using System;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_InitSet_frm_ProductData : System.Web.UI.Page
{
    public string DateTimeValidator(ArrayList DateTimeArray)
    {
        string strReturnValue = "";

        IEnumerator ienumer = DateTimeArray.GetEnumerator();

        while (ienumer.MoveNext())
        {
            CuTextSelectBox txtbox = (CuTextSelectBox)ienumer.Current;

            if (txtbox.Text.Trim() == "")
            {
                strReturnValue = txtbox.ToolTip + "时间不能为空！";

                break;
            }
            else
            {
                try
                {
                    DateTime dateTime = DateTime.Parse(txtbox.Text.Trim());
                }
                catch
                {
                    strReturnValue = txtbox.ToolTip + "时间格式不正确！";

                    break;
                }
            }
        }

        return strReturnValue;
    }

    private void ControlButtonVisible(string UserLogID, string CurrPageID)
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetCurrUserButtonPopedom(UserLogID, CurrPageID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intCount = dst.Tables[0].Rows.Count - 1;

            for (int i = intCount; i >= 0; i--)
            {
                int intButtonID = Int32.Parse(dst.Tables[0].Rows[i][2].ToString());

                switch (intButtonID)
                {
                    case 0:
                        this.panAdd.Visible = true;
                        break;
                    case 1:
                        this.hidButtonEdit.Value = intButtonID.ToString();
                        break;
                    case 2:
                        this.panDelete.Visible = true;
                        break;
                    case 3:
                        this.lbtnDelete1.Enabled = true;
                        break;
                    case 4:
                        this.panAudit.Visible = true;
                        break;
                    case 5:
                        this.hidButtonOAudit.Value = intButtonID.ToString();
                        break;
                    case 6:
                        this.panRef.Visible = true;
                        break;
                    case 7:
                        this.panSearch.Visible = true;
                        break;
                    case 20:
                        this.panPrint.Visible = true;
                        break;
                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                ////////////////按钮权限控制 Begin /////////////////////////
                string strCurrPageID = this.Page.Request.Params["PageID"];
                this.ControlButtonVisible(strUserID, strCurrPageID);
                //////////////////////  End ///////////////////////////////

                this.GetProductDatas();
            }
            else
            {
                this.lblJs.Text = "";//清空JS脚本
            }

            this.ibtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete1.Attributes.Add("onClick", "return confirm('彻底作废将无法恢复数据。您真的要作废所选的项吗？');");
            this.txtBuyprice.Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
            this.txtCostprice.Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
            this.txtBatprice.Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
            this.txtPrice.Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
            this.txtagent1.Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
            this.txtagent2.Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
            this.txtagent3.Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
        }
        else
        {
            ClsMenuDAL cls_Menu = new ClsMenuDAL();
            string strMessage = cls_Menu.GetSystemCode("1", "00", this.txt1.Text.Trim(), 41);//取消当前系统编码
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindProductDatasByDataView(dst, intPageIndex);
        }
    }

    public void AddProductDataInfo(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserIDD = cls_Property.SetOrGetUserID;

        if (strUserIDD != "")
        {
            string strPd_ID = this.txt1.Text.Trim();
            string strPd_Name = this.txt2.Text.Trim();
            string strPd_Sname = this.txtsname.Text.Trim();//简称
            string strPd_Itid = this.txtIt_id.Text.Trim();//类别编码
            int idrpMM = int.Parse(this.drpMM.SelectedValue);//是否药品
            int idrpeff = int.Parse(this.drpeff.SelectedValue);//是否启用有效期
            string dtIeff = this.txtIeff.Text.Trim();//有效期
            string strPd_Warn = this.txtWarn.Text.Trim();//预警期管理方式
            string dtWarnDate = this.txtWarnDate.Text.Trim();//预警期
            string strPd_Number = this.txtNumber.Text.Trim();//批准文号
            string dcBuyprice = this.txtBuyprice.Text.Trim();//采购价
            string dcCostprice = this.txtCostprice.Text.Trim();//成本价
            string dcBatprice = this.txtBatprice.Text.Trim();//批发价
            string dcPrice = this.txtPrice.Text.Trim();//零售价
            string dcAgent1 = this.txtagent1.Text.Trim();//一级代理价
            string dcAgent2 = this.txtagent2.Text.Trim();//二级代理价
            string dcAgent3 = this.txtagent3.Text.Trim();//三级代理价
            string strPd_Nt_id = this.ctsbRsNt_id.Text.Trim();//基本计量单位
            string strPd_Statistic1 = this.ctsbStatistic1.Text.Trim();//统计计量单位一
            string strPd_Statistic2 = this.ctsbStatistic2.Text.Trim();//统计计量单位二
            string strPd_Statistic3 = this.ctsbStatistic3.Text.Trim();//统计计量单位三
            string strPd_Statistic4 = this.ctsbStatistic4.Text.Trim();//统计计量单位四
            string strPd_Statistic5 = this.ctsbStatistic5.Text.Trim();//统计计量单位五
            string strPd_Ename = this.txtEname.Text.Trim();//品名(英文)
            string strPd_FactoryCname = this.txtfactoryCname.Text.Trim();//生产厂家(中文)
            string strPd_FactoryEname = this.txtfactoryEname.Text.Trim();//生产厂家(英文)
            string strPd_Mmtypenumber = this.txtMmtypenumber.Text.Trim();//药品分类码
            string strPd_Mmnumber = this.txtMmnumber.Text.Trim();//药品批准文号
            string strPd_Size = this.txtSize.Text.Trim();//规格
            string strPd_Mctype = this.txtMctype.Text.Trim();//剂型
            string strPd_Parea = this.txtParea.Text.Trim();//产地
            string strPd_Makeup = this.txtMakeup.Text.Trim();//主要成分
            string strPd_Uses = this.txtUses.Text.Trim();//用法用量
            string strPd_Ill = this.txtIll.Text.Trim();//适应症
            int idrpOtc = int.Parse(this.drpOtc.SelectedValue);//是否OTC 
            int idrpBatnumber = int.Parse(this.drpBatnumber.SelectedValue);//是否启用批号
            int iIdentifyNo = int.Parse(this.drpAddIdentifyNo.SelectedValue);//是否启用识别码
            string strOper = this.txtOper.Text.Trim();//录入人
            int idrpState = int.Parse(this.drpState.SelectedValue);//状态
            string strPd_Remark = this.txtRemark.Text.Trim();//备注

            //非空验证
            ArrayList txtBoxArray = new ArrayList();
            txtBoxArray.Add(this.txt1);
            txtBoxArray.Add(this.txt2);
            txtBoxArray.Add(this.txtBuyprice);
            txtBoxArray.Add(this.txtCostprice);
            txtBoxArray.Add(this.txtBatprice);
            txtBoxArray.Add(this.txtPrice);
            txtBoxArray.Add(this.txtagent1);
            txtBoxArray.Add(this.txtagent2);
            txtBoxArray.Add(this.txtagent3);

            ClsSystemSetDAL cls_System = new ClsSystemSetDAL();
            string strError = cls_System.FieldValidator(txtBoxArray);

            if (strError == "")
            {
                //时间验证
                txtBoxArray.Clear();
                txtBoxArray.Add(this.txtIeff);
                txtBoxArray.Add(this.txtWarnDate);

                strError = this.DateTimeValidator(txtBoxArray);

                if (strError == "")
                {
                    if (strPd_Itid != "")
                    {
                        if (strPd_Nt_id != "")
                        {
                            if (strPd_Statistic1 != "")
                            {
                                ClsProductDataDAL cls_ProductData = new ClsProductDataDAL();

                                Boolean bolIsCussee = cls_ProductData.AddProductData(
                                    strPd_ID, strPd_Name, strPd_Sname, strPd_Itid, idrpMM, idrpeff, DateTime.Parse(dtIeff), strPd_Warn, DateTime.Parse(dtWarnDate), strPd_Number, Decimal.Parse(dcBuyprice), Decimal.Parse(dcCostprice),
                                    Decimal.Parse(dcBatprice), Decimal.Parse(dcPrice), Decimal.Parse(dcAgent1), Decimal.Parse(dcAgent2), Decimal.Parse(dcAgent3), strPd_Nt_id, strPd_Statistic1, strPd_Statistic2, strPd_Statistic3, strPd_Statistic4, strPd_Statistic5,
                                    strPd_Ename, strPd_FactoryCname, strPd_FactoryEname, strPd_Mmtypenumber, strPd_Mmnumber, strPd_Size, strPd_Mctype, strPd_Parea, strPd_Makeup, strPd_Uses,
                                    strPd_Ill, idrpOtc, idrpBatnumber, iIdentifyNo, strUserIDD, idrpState, strPd_Remark);

                                if (bolIsCussee)
                                {
                                    this.GetProductDatas();//重新绑定

                                    this.txt2.Text = this.txtsname.Text = this.txtIt_id.Text = this.txtWarn.Text = this.txtNumber.Text = "";
                                    this.ctsbRsNt_id.Text= this.ctsbStatistic1.Text = this.ctsbStatistic2.Text = this.ctsbStatistic3.Text = this.ctsbStatistic4.Text = this.ctsbStatistic5.Text = "";
                                    this.txtEname.Text = this.txtfactoryCname.Text = this.txtfactoryEname.Text = this.txtMmtypenumber.Text = this.txtMmnumber.Text = "";
                                    this.txtSize.Text = this.txtMctype.Text = this.txtParea.Text = this.txtMakeup.Text = this.txtUses.Text = this.txtIll.Text = this.txtRemark.Text = "";
                                    this.txtBuyprice.Text = this.txtCostprice.Text = this.txtBatprice.Text = this.txtPrice.Text = this.txtagent1.Text = this.txtagent2.Text = this.txtagent3.Text = "0";

                                    ClsMenuDAL cls_Menu = new ClsMenuDAL();

                                    string strCode = cls_Menu.GetSystemCode("0", "00", "", 41);

                                    if (strCode != "Error")
                                    {
                                        this.txt1.Text = strCode;
                                        this.lblJs.Text = "<script>document.getElementById('" + this.txt2.ClientID + "').select();ocument.getElementById('" + this.txt2.ClientID + "').focus()</script>";
                                    }
                                    else
                                    {
                                        this.panAddRs.Visible = false;
                                    }
                                }
                                else
                                {
                                    this.lblJs.Text = "<script>alert('新增商品资料信息失败，请重试！')</script>";
                                }
                            }
                            else
                            {
                                this.lblJs.Text = "<script>alert('统计计量单位一不能为空！')</script>";
                            }
                        }
                        else
                        {
                            this.lblJs.Text = "<script>alert('基本计量单位不能为空！')</script>";
                        }
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('商品类别不能为空！')</script>";
                    }
                }
                else
                {
                    this.lblJs.Text = "<script>alert('" + strError + "')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('" + strError + "')</script>";
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void UpDateProductDataInfo(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserIDD = cls_Property.SetOrGetUserID;

        if (strUserIDD != "")
        {
            ClsProductDataDAL cls_ProductData = new ClsProductDataDAL();

            ImageButton ibtn = (ImageButton)sender;

            int intCommandName = Int32.Parse(ibtn.CommandName);

            if (intCommandName == 1)//确认
            {
                int intCount = this.dgrdResult.Items.Count;

                for (int i = 0; i < intCount; i++)
                {
                    CheckBox chkBox = (CheckBox)this.dgrdResult.Items[i].FindControl("chkSelect");

                    if (chkBox.Checked)
                    {
                        bool is_audit = bool.Parse(this.dgrdResult.Items[i].Cells[7].Text.Trim());

                        if (!is_audit)
                        {
                            TextBox txtBox = (TextBox)this.dgrdResult.Items[i].FindControl("im_no");

                            Boolean bolIsCussee = cls_ProductData.AuditStatProductData(txtBox.Text.Trim(), intCommandName, strUserIDD);

                            if (!bolIsCussee)
                            {
                                this.lblJs.Text = "<script>alert('确认商品资料信息失败，请重试！')</script>";
                            }
                        }
                        else
                        { 
                            this.lblJs.Text = "<script>alert('此记录已确认！')</script>";
                        }
                    }
                }

                this.GetProductDatas();//重新绑定
            }
            else//取消确认，编辑，取消编辑，保存编辑
            {
                int intIndex = Int32.Parse(ibtn.CommandArgument);

                TextBox im_no = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_no");
                TextBox im_name = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_name");
                TextBox im_sname = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_sname");//
                TextBox txtMM = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtMM");
                TextBox txteff = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txteff");
                TextBox im_warnman = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_warnman");
                TextBox im_number = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_number");
                TextBox im_buyprice = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_buyprice");
                TextBox cost_price = (TextBox)this.dgrdResult.Items[intIndex].FindControl("cost_price");
                TextBox im_batprice = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_batprice");
                TextBox price = (TextBox)this.dgrdResult.Items[intIndex].FindControl("price");
                TextBox im_agent1 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_agent1");
                TextBox im_agent2 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_agent2");
                TextBox im_agent3 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_agent3");
                TextBox im_ename = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_ename");
                TextBox im_factoryCname = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_factoryCname");
                TextBox im_factoryEname = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_factoryEname");
                TextBox im_mmtypenumber = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_mmtypenumber");
                TextBox im_mmnumber = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_mmnumber");
                TextBox im_size = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_size");
                TextBox im_mctype = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_mctype");
                TextBox im_parea = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_parea");
                TextBox im_makeup = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_makeup");
                TextBox im_uses = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_uses");
                TextBox im_ill = (TextBox)this.dgrdResult.Items[intIndex].FindControl("im_ill");
                TextBox txtOtc = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtOtc");
                TextBox txtBatnumber = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtBatnumber");
                TextBox txtState = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtState");
                TextBox remark = (TextBox)this.dgrdResult.Items[intIndex].FindControl("remark");
                
                Label labRsIdentifyNo = (Label)this.dgrdResult.Items[intIndex].FindControl("labRsIdentifyNo");//是否启用识别码
                DropDownList drpRsIdentifyNo = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("drpRsIdentifyNo");//是否启用识别码
                DropDownList drpMM = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("drpMM");
                DropDownList drpeff = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("drpeff");
                DropDownList drpOtc = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("drpOtc");
                DropDownList drpBatnumber = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("drpBatnumber");
                DropDownList drpState = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("drpState");
                
                CuTextBox it_name = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("it_name");//
                CuTextSelectBox it_id = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("it_id");
                CuTextSelectBox ctsbIeff = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbIeff");
                CuTextSelectBox ctsbWarndate = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbWarndate");
                CuTextSelectBox ctsbRsNt_id = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRsNt_id");
                CuTextSelectBox ctsbStatistic1 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbStatistic1");
                CuTextSelectBox ctsbStatistic2 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbStatistic2");
                CuTextSelectBox ctsbStatistic3 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbStatistic3");
                CuTextSelectBox ctsbStatistic4 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbStatistic4");
                CuTextSelectBox ctsbStatistic5 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbStatistic5");
                
                CheckBox chk = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkSelect");
                ImageButton ibtnEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnEdit");
                ImageButton ibtnCan = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnCan");
                ImageButton ibtnSer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnSer");
                ImageButton ibtnOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnOAudit");

                if (intCommandName == 0)//取消确认
                {
                    Boolean bolIsCussee = cls_ProductData.AuditStatProductData(im_no.Text.Trim(), intCommandName, strUserIDD);

                    if (bolIsCussee)
                    {
                        this.GetProductDatas();//重新绑定
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('取消确认商品资料信息失败，请重试！')</script>";
                    }
                }
                else if (intCommandName == 2)//编辑
                {
                    it_id.Text = it_id.ToolTip;
                    ctsbRsNt_id.Text = ctsbRsNt_id.ToolTip;
                    ctsbStatistic1.Text = ctsbStatistic1.ToolTip;
                    ctsbStatistic2.Text = ctsbStatistic2.ToolTip;
                    ctsbStatistic3.Text = ctsbStatistic3.ToolTip;
                    ctsbStatistic4.Text = ctsbStatistic4.ToolTip;
                    ctsbStatistic5.Text = ctsbStatistic5.ToolTip;

                    ibtnCan.Visible = ibtnSer.Visible = true;
                    drpRsIdentifyNo.Visible = drpMM.Visible = drpeff.Visible = drpOtc.Visible = drpBatnumber.Visible = drpState.Visible = true;
                    chk.Visible = ibtnEdit.Visible = ibtnOAudit.Visible = false;
                    txtMM.Visible = txteff.Visible = txtOtc.Visible = txtBatnumber.Visible = labRsIdentifyNo.Visible = txtState.Visible = false;

                    im_name.ReadOnly = im_sname.ReadOnly = im_warnman.ReadOnly = im_number.ReadOnly = false;
                    im_buyprice.ReadOnly = cost_price.ReadOnly = im_batprice.ReadOnly = price.ReadOnly = im_agent1.ReadOnly = im_agent2.ReadOnly = false;
                    im_agent3.ReadOnly = im_ename.ReadOnly = im_factoryCname.ReadOnly = im_factoryEname.ReadOnly = im_mmtypenumber.ReadOnly = false;
                    im_mmnumber.ReadOnly = im_size.ReadOnly = im_mctype.ReadOnly = im_parea.ReadOnly = im_makeup.ReadOnly = im_uses.ReadOnly = false;
                    im_ill.ReadOnly = remark.ReadOnly = false;
                    it_id.ReadOnly = ctsbIeff.ReadOnly = ctsbWarndate.ReadOnly = ctsbRsNt_id.ReadOnly = ctsbStatistic1.ReadOnly = false;
                    ctsbStatistic2.ReadOnly = ctsbStatistic3.ReadOnly = ctsbStatistic4.ReadOnly = ctsbStatistic5.ReadOnly = false;

                    im_name.BorderWidth = im_sname.BorderWidth = im_warnman.BorderWidth = im_number.BorderWidth = 1;
                    im_buyprice.BorderWidth = cost_price.BorderWidth = im_batprice.BorderWidth = price.BorderWidth = im_agent1.BorderWidth = im_agent2.BorderWidth = 1;
                    im_agent3.BorderWidth = im_ename.BorderWidth = im_factoryCname.BorderWidth = im_factoryEname.BorderWidth = im_mmtypenumber.BorderWidth = 1;
                    im_mmnumber.BorderWidth = im_size.BorderWidth = im_mctype.BorderWidth = im_parea.BorderWidth = im_makeup.BorderWidth = im_uses.BorderWidth = 1;
                    im_ill.BorderWidth = remark.BorderWidth = 1;


                    //让第一个文本框获取焦点
                    this.lblJs.Text = "<script>document.getElementById('" + im_name.ClientID + "').select();document.getElementById('" + im_name.ClientID + "').focus()</script>";
                }
                else if (intCommandName == 3)//取消编辑
                {
                    this.GetProductDatas();
                }
                else//保存编辑
                {
                    //非空验证
                    ArrayList txtBoxArray = new ArrayList();
                    txtBoxArray.Add(im_name);
                    txtBoxArray.Add(im_buyprice);
                    txtBoxArray.Add(cost_price);
                    txtBoxArray.Add(im_batprice);
                    txtBoxArray.Add(price);
                    txtBoxArray.Add(im_agent1);
                    txtBoxArray.Add(im_agent2);
                    txtBoxArray.Add(im_agent3);
                    
                    ClsSystemSetDAL cls_System = new ClsSystemSetDAL();
                    string strError = cls_System.FieldValidator(txtBoxArray);

                    if (strError == "")
                    {
                        //时间验证
                        txtBoxArray.Clear();
                        txtBoxArray.Add(ctsbIeff);
                        txtBoxArray.Add(ctsbWarndate);

                        strError = this.DateTimeValidator(txtBoxArray);

                        if (strError == "")
                        {
                            if (it_id.Text.Trim() != "")
                            {
                                if (ctsbRsNt_id.Text.Trim() != "")
                                {
                                    if (ctsbStatistic1.Text.Trim() != "")
                                    {
                                        Boolean bolIsCussee = cls_ProductData.UpDateProductData(
                                        im_no.Text.Trim(), im_name.Text.Trim(), im_sname.Text.Trim(), it_id.Text.Trim(), int.Parse(drpMM.SelectedValue), int.Parse(drpeff.SelectedValue),
                                        DateTime.Parse(ctsbIeff.Text.Trim()), im_warnman.Text.Trim(), DateTime.Parse(ctsbWarndate.Text.Trim()), im_number.Text.Trim(), Decimal.Parse(im_buyprice.Text.Trim()),
                                        Decimal.Parse(cost_price.Text.Trim()), Decimal.Parse(im_batprice.Text.Trim()), Decimal.Parse(price.Text.Trim()), Decimal.Parse(im_agent1.Text.Trim()), Decimal.Parse(im_agent2.Text.Trim()),
                                        Decimal.Parse(im_agent3.Text.Trim()), ctsbRsNt_id.Text.Trim(), ctsbStatistic1.Text.Trim(), ctsbStatistic2.Text.Trim(), ctsbStatistic3.Text.Trim(), ctsbStatistic4.Text.Trim(),
                                        ctsbStatistic5.Text.Trim(), im_ename.Text.Trim(), im_factoryCname.Text.Trim(), im_factoryEname.Text.Trim(), im_mmtypenumber.Text.Trim(),
                                        im_mmnumber.Text.Trim(), im_size.Text.Trim(), im_mctype.Text.Trim(), im_parea.Text.Trim(), im_makeup.Text.Trim(),
                                        im_uses.Text.Trim(), im_ill.Text.Trim(), int.Parse(drpOtc.SelectedValue), int.Parse(drpBatnumber.SelectedValue), int.Parse(drpRsIdentifyNo.SelectedValue), int.Parse(drpState.SelectedValue), remark.Text.Trim());

                                        if (bolIsCussee)
                                        {
                                            this.lblJs.Text = "";
                                            this.GetProductDatas();//重新绑定
                                        }
                                        else
                                        {
                                            this.lblJs.Text = "<script>alert('修改商品资料信息失败，请重试！')</script>";
                                        }
                                    }
                                    else
                                    {
                                        this.lblJs.Text = "<script>alert('统计计量单位一不能为空！')</script>";
                                    }
                                }
                                else
                                {
                                    this.lblJs.Text = "<script>alert('基本计量单位不能为空！')</script>";
                                }
                            }
                            else
                            {
                                this.lblJs.Text = "<script>alert('商品类别不能为空！')</script>";
                            }
                        }
                        else
                        {
                            this.lblJs.Text = "<script>alert('" + strError + "')</script>";
                        }
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('" + strError + "')</script>";
                    }
                }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void lbtnDelete_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteProductDataInfo(intCommandName);
    }

    private void DeleteProductDataInfo(int CommandName)
    {
        ClsProductDataDAL cls_ProductData = new ClsProductDataDAL();

        if (CommandName == 1 || CommandName == -1)
        {
            int intCount = this.dgrdResult.Items.Count;

            for (int i = 0; i < intCount; i++)
            {
                CheckBox chkBox = (CheckBox)this.dgrdResult.Items[i].FindControl("chkSelect");

                if (chkBox.Checked)
                {
                    bool is_audit = bool.Parse(this.dgrdResult.Items[i].Cells[7].Text.Trim());

                    if (!is_audit)
                    {
                        TextBox txtBox = (TextBox)this.dgrdResult.Items[i].FindControl("im_no");

                        Boolean bolIsCussee = cls_ProductData.DeleteProductData(txtBox.Text.Trim(), CommandName);

                        if (!bolIsCussee)
                        {
                            this.lblJs.Text = "<script>alert('作废商品资料信息失败，请重试！')</script>";
                        }
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('此记录已确认，不能作废！')</script>";
                    }
                }
            }
        }

        this.GetProductDatas();
    }
  
    private void GetProductDatas()
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsProductDataDAL cls_ProductData = new ClsProductDataDAL();
            DataSet dst = cls_ProductData.GetProductDatas("00", strUserID, "DS000202");

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                cls_Property.SetOrGetCorporationDataSet = dst;

                this.BindProductDatasByDataView(dst, 0);
            }
            else
            {
                this.dgrdResult.CurrentPageIndex = 0;
                this.dgrdResult.DataSource = null;
                this.dgrdResult.DataBind();
                this.lblRsCounts.Text = "共0条记录";
                this.lblPageCounts.Text = "共0页";
                this.lblCurrPage.Text = "当前第0页";
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
                this.lblRsCounts.ForeColor = this.lblPageCounts.ForeColor = this.lblCurrPage.ForeColor = this.lblGoTxt1.ForeColor = this.lblGoTxt2.ForeColor = Color.FromArgb(172, 168, 153);
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='index.aspx'</script>";
        }
    }

    private void BindProductDatasByDataView(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.ForeColor = this.lblPageCounts.ForeColor = this.lblCurrPage.ForeColor = this.lblGoTxt1.ForeColor = this.lblGoTxt2.ForeColor = Color.FromArgb(78, 78, 78);

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
                this.lblGoTxt1.ForeColor = this.lblGoTxt2.ForeColor = Color.FromArgb(172, 168, 153);
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
    }    

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        string strKey = this.txtKey.Text.Trim();

        if (strKey != "" && strKey != "查询关键字")
        {
            ClsProductDataDAL cls_ProductData = new ClsProductDataDAL();
            DataSet dst = cls_ProductData.SearchProductDatas(strKey);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                cls_Property.SetOrGetCorporationDataSet = dst;

                this.BindProductDatasByDataView(dst, 0);
            }
            else
            {
                this.lblJs.Text = "<script>alert('搜索结果为空！')</script>";
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('请输入搜索关键字！')</script>";
        }
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindProductDatasByDataView(dst, intIndex);
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('请输入正确的页码数！')</script>";
                    }
                }
                catch
                {
                    this.lblJs.Text = "<script>alert('请输入数字类型的页码！')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('请输入要跳转的页码！')</script>";
            }
        }
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Boolean bIs_mm = Boolean.Parse(e.Item.Cells[1].Text.Trim());
            Boolean bIs_effectdate = Boolean.Parse(e.Item.Cells[2].Text.Trim());
            Boolean bIs_otc = Boolean.Parse(e.Item.Cells[3].Text.Trim());
            Boolean bIs_batnumber = Boolean.Parse(e.Item.Cells[4].Text.Trim());
            Boolean bIs_stop = Boolean.Parse(e.Item.Cells[5].Text.Trim());
            Boolean bIs_IdentifyNo = Boolean.Parse(e.Item.Cells[8].Text.Trim());//是否启用识别码

            string strIs_audit = e.Item.Cells[7].Text.Trim();

            CheckBox chkbox = (CheckBox)e.Item.FindControl("chkSelect");
            TextBox remark = (TextBox)e.Item.FindControl("remark");
            ImageButton ibtnEdit = (ImageButton)e.Item.FindControl("ibtnEdit");
            ImageButton ibtnCan = (ImageButton)e.Item.FindControl("ibtnCan");
            ImageButton ibtnSer = (ImageButton)e.Item.FindControl("ibtnSer");
            ImageButton ibtnOAudit = (ImageButton)e.Item.FindControl("ibtnOAudit");
            CheckBox chkVirtual = (CheckBox)e.Item.FindControl("is_virtual");

            System.Web.UI.WebControls.Image is_audit = (System.Web.UI.WebControls.Image)e.Item.FindControl("is_audit");            

            TextBox txtMM = (TextBox)e.Item.FindControl("txtMM");
            DropDownList drpMM = (DropDownList)e.Item.FindControl("drpMM");
            TextBox txteff = (TextBox)e.Item.FindControl("txteff");
            DropDownList drpeff = (DropDownList)e.Item.FindControl("drpeff");
            TextBox txtOtc = (TextBox)e.Item.FindControl("txtOtc");
            DropDownList drpOtc = (DropDownList)e.Item.FindControl("drpOtc");
            TextBox txtBatnumber = (TextBox)e.Item.FindControl("txtBatnumber");
            DropDownList drpBatnumber = (DropDownList)e.Item.FindControl("drpBatnumber");
            TextBox txtState = (TextBox)e.Item.FindControl("txtState");
            DropDownList drpState = (DropDownList)e.Item.FindControl("drpState");
            Label labIdentifyNo = (Label)e.Item.FindControl("labRsIdentifyNo");    //是否启用识别码
            DropDownList drpRsIdentifyNo = (DropDownList)e.Item.FindControl("drpRsIdentifyNo");

            drpMM.SelectedIndex = (!bIs_mm) ? 0 : 1;
            txtMM.Text = txtMM.ToolTip = drpMM.SelectedItem.Text;
            drpeff.SelectedIndex = (!bIs_effectdate) ? 0 : 1;
            txteff.Text = txteff.ToolTip = drpeff.SelectedItem.Text;
            drpOtc.SelectedIndex = (!bIs_otc) ? 0 : 1;
            txtOtc.Text = txtOtc.ToolTip = drpOtc.SelectedItem.Text;
            drpBatnumber.SelectedIndex = (!bIs_batnumber) ? 0 : 1;
            txtBatnumber.Text = txtBatnumber.ToolTip = drpBatnumber.SelectedItem.Text;
            drpState.SelectedIndex = (!bIs_stop) ? 0 : 1;
            txtState.Text = txtState.ToolTip = drpState.SelectedItem.Text;
            drpRsIdentifyNo.SelectedIndex = (!bIs_IdentifyNo) ? 0 : 1;    //是否启用识别码
            labIdentifyNo.Text = labIdentifyNo.ToolTip = drpRsIdentifyNo.SelectedItem.Text;

            string strButtonEdit = this.hidButtonEdit.Value;
            string strButtonOAudit = this.hidButtonOAudit.Value;

            //修改按回车提交
            if (remark.ReadOnly)
            {
                ((TextBox)e.Item.FindControl("im_buyprice")).Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
                ((TextBox)e.Item.FindControl("cost_price")).Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
                ((TextBox)e.Item.FindControl("im_batprice")).Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
                ((TextBox)e.Item.FindControl("price")).Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
                ((TextBox)e.Item.FindControl("im_agent1")).Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
                ((TextBox)e.Item.FindControl("im_agent2")).Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
                ((TextBox)e.Item.FindControl("im_agent3")).Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");

                remark.Attributes.Add("onkeydown", "if(event.keyCode == 13){document.getElementById('" + ibtnSer.ClientID + "').click();}");
            }

            if (strIs_audit == "False")
            {
                if (strButtonEdit.Trim() == "1")
                {
                    if (this.panAudit.Visible || this.panDelete.Visible)
                    {
                        chkbox.Visible = true;
                    }

                    ibtnEdit.Visible = true;
                }
                else if (this.panAudit.Visible || this.panDelete.Visible)
                {
                    chkbox.Visible = true;
                }
            }
            else
            {
                ibtnOAudit.Visible = true;

                if (strButtonOAudit.Trim() != "5")
                {
                    ibtnOAudit.Enabled = false;
                    ibtnOAudit.ImageUrl = "../App_Images/ico/17.gif";
                }
            }
        }
    }

    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int intCommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteProductDataInfo(intCommandName);
    }
   
    protected void imgAdd_Click(object sender, ImageClickEventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsMenuDAL cls_Menu = new ClsMenuDAL();

            string strCode = cls_Menu.GetSystemCode("0", "00", "", 41);

            if (strCode != "Error")
            {
                this.txt1.Text = strCode;
                this.panAddRs.Visible = true;
                this.txtOper.Text = strUserID;
                this.txtIeff.Text = this.txtWarnDate.Text = this.txtOperdate.Text = DateTime.Now.ToString();
                this.txtBuyprice.Text = this.txtCostprice.Text = this.txtBatprice.Text = this.txtPrice.Text = this.txtagent1.Text = this.txtagent2.Text = this.txtagent3.Text = "0";
                this.lblJs.Text = "<script>document.getElementById('" + this.txt2.ClientID + "').focus();</script>";
            }
            else
            {
                this.lblJs.Text = "<script>alert('添加记录失败，请重试！')</script>";
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void imgCancel_Click(object sender, ImageClickEventArgs e)
    {
        this.panAddRs.Visible = false;

        ClsMenuDAL cls_Menu = new ClsMenuDAL();

        string strCode = cls_Menu.GetSystemCode("1", "00", txt1.Text.Trim(), 41);
    }

    protected void imgRef_Click(object sender, ImageClickEventArgs e)
    {
        this.GetProductDatas();
    }
}
